class ArticlesController < ApplicationController
  # GET /articles
  # GET /articles.xml
  layout "admin"
  before_filter :admin?,:except=>[:show,:search,:list]
  cache_sweeper :link_sweeper,:only=>[:create,:destroy,:update]
  def index
    @articles = Article.paginate(:page=>params[:page],:per_page=>15,:conditions=>(["article_category_id=?",params[:article_category_id]] unless params[:article_category_id]=="0") )
    load_data unless request.xhr?
    respond_to do |format|
      format.html # index.html.erb
      format.js {render :partial=>"index_list"}
      format.xml  { render :xml => @articles }
    end
  end

  def list
    @cate=ArticleCategory.find(params[:article_category_id])
    @page_title=@cate.name
    
    respond_to do |format|
      format.html {render :layout=>'front_page'}
      format.xml  { render :xml => @articles }
    end
  end

  def search
    @articles=Article.paginate(:conditions=>["title like ?","%#{params[:title]}%"],:page=>params[:page],:per_page=>15,:order=>"created_at desc")
    @page_title="搜索结果"
    @page_nav=">&nbsp;#{@page_title}"
    respond_to do |format|
      format.html {render :layout=>'front_page'}
      format.js {render :partial=>"index_list"}
      format.xml  { render :xml => @articles }
    end
  end

  # GET /articles/1
  # GET /articles/1.xml
  def show
    @article = Article.find(params[:id])
    @page_des=@article.short_description
    @page_key=@article.key_word
    @page_title=@article.title

    @parent_cate=ArticleCategory.find_by_sql(["select a.id cate_id,a.parent_id,a.name,b.name parent_name from article_categories a
       inner join article_categories b on a.parent_id=b.id where a.id=?",@article.article_category_id])[0]
    @page_nav="><a href='#{list_article_category_articles_path(@parent_cate.parent_id)}'>#{ @parent_cate.parent_name}</a>"
    @page_nav+="><a href='#{list_article_category_articles_path(@parent_cate.cate_id)}'>#{@parent_cate.name}</a>>&nbsp;正文"

    @article.increment!("click_count",1)
    respond_to do |format|
      format.html {render :action=>:show,:layout=>"front_page"}
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new
    load_data
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
    load_data
  end

  # POST /articles
  # POST /articles.xml
  def create
    @article = Article.new(params[:article])
    @article.article_content=ArticleContent.new(params[:article_content])
    respond_to do |format|
      if @article.save
        flash[:notice] = '添加成功！'
        load_data
        format.html { redirect_to new_article_category_article_path(0) }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  def update
    @article = Article.find(params[:id])
    @article.article_content.blank? ? @article.article_content=ArticleContent.new(params[:article_content]):(@article.article_content.update_attribute("content",params[:article_content][:content]))

    respond_to do |format|
      if @article.update_attributes(params[:article])
        flash[:notice] = '更新成功!'
        format.html { redirect_to(article_category_articles_path(0)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    @articles = Article.paginate(:page=>params[:page],:per_page=>15,:conditions=>(["article_category_id=?",params[:article_category_id]] unless params[:article_category_id]=="0") )

    respond_to do |format|
      format.html { render :action=>:index }
      format.xml  { head :ok }
    end
  end

  private
  def load_data
    @cate||=ArticleCategory.all(:conditions=>"parent_id is not null")
  end
end
